package com.sgzn.sgpay.service.tcp.receive.handler;

import com.sgzn.sgpay.service.tcp.receive.TcpReceiveCallHandlerTemplate;
import com.sgzn.sgpay.service.tcp.receive.modle.TcpReceiveCallRequest;
import com.sgzn.sgpay.service.tcp.receive.modle.response.TcpQueryTerminalParamBabyResult;
import com.sgzn.sgpay.service.tcp.receive.modle.response.TcpQueryTerminalParamExchangeResult;
import com.sgzn.sgpay.service.tcp.receive.modle.response.TcpQueryTerminalParamResult;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 查询终端参数应答 处理器
 * @author huangsenguang
 * @version Id: TcpQueryTerminalParamHandler.java, v 0.1 2018/7/29 15:45 huangsenguang Exp $$
 */
@Service
@Slf4j
public class TcpQueryTerminalParamHandler extends TcpReceiveCallHandlerTemplate<TcpQueryTerminalParamResult> {
    /**
     * 协议总字符数 娃娃机
     */
    @Getter
    private final int babyProtocolLength = 46;

    /**
     * 协议总字符数 兑币机
     */
    @Getter
    private final int exchangeProtocolLength = 36;
    private static final String POINT = ".";

    @Override
    public TcpQueryTerminalParamResult callInner(TcpReceiveCallRequest request) {
        String data = request.getData();
        TcpQueryTerminalParamResult result = new TcpQueryTerminalParamResult();
        if (request.getProtocolContext().length() == this.babyProtocolLength) {
            //娃娃机
            handleBaby(data, result);
        } else {
            //兑币机
            handleExchange(data, result);
        }
        return result;
    }

    /**
     * 处理娃娃机 data
     * @param data
     * @param result
     */
    private void handleBaby(String data, TcpQueryTerminalParamResult result) {
        TcpQueryTerminalParamBabyResult babyResult = new TcpQueryTerminalParamBabyResult();
        babyResult.setGameModle(parse(data, 0, 2));
        babyResult.setAwardProbability(parse(data, 2, 6));
        babyResult.setPowerfulVoltage(String.valueOf(parse(data, 6, 8)+ POINT +String.valueOf(parse(data,8,10))));
        babyResult.setWeakVoltage(String.valueOf(parse(data, 10, 12)+ POINT +String.valueOf(parse(data,12,14))));
        babyResult.setPowerToWeakTime(String.valueOf(parse(data, 14, 16)+ POINT +String.valueOf(parse(data,16,18))));
        babyResult.setToTopConvertWeakForce(parse(data, 18, 20));
        babyResult.setCoinPlay(parse(data, 20, 22));
        babyResult.setPlayTime(parse(data, 22, 24));
        babyResult.setMusic(parse(data, 24, 26));
        babyResult.setUnCatchFillCoin(parse(data, 26, 28));
        babyResult.setKeyForCatch(parse(data, 28, 30));
        babyResult.setContinueAwardCoin(parse(data, 30, 32));
        babyResult.setGameAutoStart(parse(data, 32, 34));
        result.setBabyResult(babyResult);
    }

    /**
     * 处理兑币机 data
     * @param data
     * @param result
     */
    private void handleExchange(String data, TcpQueryTerminalParamResult result) {
        TcpQueryTerminalParamExchangeResult exchangeResult = new TcpQueryTerminalParamExchangeResult();
        exchangeResult.setOneToCoin(parse(data, 0, 4));
        exchangeResult.setFiveToCoin(parse(data, 4, 8));
        exchangeResult.setTenToCoin(parse(data, 8, 12));
        exchangeResult.setTwentyToCoin(parse(data, 12, 16));
        exchangeResult.setFiftyToCoin(parse(data, 16, 20));
        exchangeResult.setHundredToCoin(parse(data, 20, 24));
        result.setExchangeResult(exchangeResult);
    }

    @Override
    protected int getProtocolLength(TcpReceiveCallRequest request) {
        if (request.getProtocolContext().length() == this.babyProtocolLength) {
            return this.babyProtocolLength;
        }
        return this.exchangeProtocolLength;
    }

    private int parse(String data, int beginIndex, int endIndex) {
        return Integer.parseInt(SgTcpUtil.sortTcp(data.substring(beginIndex, endIndex)), 16);
    }
}

